import win32com.client.gencache

XLSX_VERSION = 51


class Xls2XlsxConverter:

    def __init__(self):
        self.excel_app = win32com.client.gencache.EnsureDispatch('Excel.Application')

    def xls_to_xlsx(self, source_file_path: str, target_file_path: str):
        """
        xls转xlsx
        :param source_file_path: 文件目录
        :param target_file_path:
        :return:
        """
        # 方案1：excel/wps程序必须关闭
        wb = self.excel_app.Workbooks.Open(source_file_path)
        wb.SaveAs(target_file_path+"x", FileFormat=XLSX_VERSION)
        wb.Close()

    def close(self):
        self.excel_app.Application.Quit()
